package com.mf.entity;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

/**
 * 持仓表
 *
 * @author mf
 * @since 2024-06-08
 */
@Getter
@Setter
@TableName("t_positions")
@ToString
public class Positions implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 价格
     */
    @NotNull(message = "价格不能为空")
    private BigDecimal price;

    /**
     * 金额
     */
    @NotNull(message = "金额不能为空")
    private BigDecimal amount;

    /**
     * 买入时间
     */
    @NotBlank(message = "买入时间不能为空")
    private String date;

    /**
     * 股票代码
     */
    @NotBlank(message = "股票代码不能为空")
    private String dm;

    /**
     * 持股数量
     */
    @NotNull(message = "持股数量不能为空")
    private Integer quantity;

    /**
     * 买入类型
     */
    @NotBlank(message = "买入类型数据字典中 buyType")
    private String type2;


    /**
     * 用户id
     */
    @NotBlank(message = "用户id不能为空")
    private Integer userId;


    private LocalDateTime createdTime;

    private LocalDateTime updateTime;

    public static Positions merge(Positions p1, Positions p2) {
        Positions merged = new Positions();
        BeanUtil.copyProperties(p1, merged);
        String str = merged.getType2() + "," + p2.getType2();
        merged.setType2(str);
        merged.setQuantity(merged.getQuantity() + (p2.getQuantity()));
        merged.setAmount(merged.getAmount().add(p2.getAmount()));
        merged.setPrice(merged.getAmount().divide(BigDecimal.valueOf(merged.getQuantity()), 2, RoundingMode.HALF_UP));
        return merged;
    }
}